package org.gdstash.ui;

import org.gdstash.db.DBStashItem;
import org.gdstash.item.GDItem;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class GDItemInfoPane extends AdjustablePanel {
    private JLabel lblImage;
    private JLabel lblFullName;
    private JLabel lblSet;
    private JLabel lblSeed;
    private GDItemInfoSimilarTablePane pnlSimilar;
    private JLabel txtFullName;
    private JLabel txtSet;
    private JLabel txtSeed;
    private JLabel txtDescription;

    public GDItemInfoPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel panel = this.buildInfoPanel();
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage).addComponent(this.pnlSimilar).addComponent(panel).addComponent(this.txtDescription));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage)).addGroup(layout.createParallelGroup().addComponent(this.pnlSimilar)).addGroup(layout.createParallelGroup().addComponent(panel)).addGroup(layout.createParallelGroup().addComponent(this.txtDescription));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblImage, this.pnlSimilar);
        layout.linkSize(0, this.lblImage, panel);
        layout.linkSize(0, this.lblImage, this.txtDescription);
    }

    public void setWidth(final int w) {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        int i = w - 4 * size;
        if (i < size) {
            i = size;
        }
        final Dimension dimMax = new Dimension(i, 2 * size);
        this.txtFullName.setPreferredSize(dimMax);
        this.txtFullName.setMaximumSize(dimMax);
        this.invalidate();
        this.doLayout();
        this.repaint();
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(37 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        fntLabel = fntLabel.deriveFont((float) size);
        Border compound = null;
        if (this.txtFullName == null || this.txtSet == null || this.txtSeed == null) {
            final Border lowered = BorderFactory.createEtchedBorder(1);
            final Border raised = BorderFactory.createEtchedBorder(0);
            compound = BorderFactory.createCompoundBorder(raised, lowered);
        }
        if (this.lblImage == null) {
            (this.lblImage = new JLabel()).setBorder(compound);
            this.lblImage.setHorizontalAlignment(0);
        }
        if (this.lblFullName == null) {
            this.lblFullName = new JLabel();
        }
        this.lblFullName.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_NAME"));
        this.lblFullName.setFont(fntLabel);
        if (this.txtFullName == null) {
            (this.txtFullName = new JLabel()).setBorder(compound);
        }
        this.txtFullName.setFont(fntLabel);
        this.txtFullName.setPreferredSize(dimMax);
        this.txtFullName.setMaximumSize(dimMax);
        if (this.lblSet == null) {
            this.lblSet = new JLabel();
        }
        this.lblSet.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SET"));
        this.lblSet.setFont(fntLabel);
        if (this.txtSet == null) {
            (this.txtSet = new JLabel()).setBorder(compound);
        }
        this.txtSet.setFont(fntLabel);
        if (this.lblSeed == null) {
            this.lblSeed = new JLabel();
        }
        this.lblSeed.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SEED"));
        this.lblSeed.setFont(fntLabel);
        if (this.txtSeed == null) {
            (this.txtSeed = new JLabel()).setBorder(compound);
        }
        this.txtSeed.setFont(fntLabel);
        if (this.pnlSimilar == null) {
            this.pnlSimilar = new GDItemInfoSimilarTablePane();
        } else {
            this.pnlSimilar.adjustUI();
        }
        if (this.txtDescription == null) {
            (this.txtDescription = new JLabel()).setBorder(compound);
        }
        this.txtDescription.setFont(fntLabel);
    }

    public void refresh() {
        this.setItemInfo(null, false, "");
    }

    private JPanel buildInfoPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblFullName).addComponent(this.lblSet).addComponent(this.lblSeed)).addGroup(layout.createParallelGroup().addComponent(this.txtFullName).addComponent(this.txtSet).addComponent(this.txtSeed));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblFullName).addComponent(this.txtFullName)).addGroup(layout.createParallelGroup().addComponent(this.lblSet).addComponent(this.txtSet)).addGroup(layout.createParallelGroup().addComponent(this.lblSeed).addComponent(this.txtSeed));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblFullName, this.lblSet);
        layout.linkSize(0, this.lblFullName, this.lblSeed);
        layout.linkSize(0, this.txtFullName, this.txtSet);
        layout.linkSize(0, this.txtFullName, this.txtSeed);
        layout.linkSize(1, this.lblFullName, this.txtFullName);
        layout.linkSize(1, this.lblSet, this.txtSet);
        layout.linkSize(1, this.lblSeed, this.txtSeed);
        return panel;
    }

    private void setInfo(final GDItem.LabelInfo info, final JLabel label) {
        if (info == null) {
            label.setText("");
        } else {
            label.setForeground(info.foreground);
            label.setText(info.text);
        }
    }

    public void setFullNameInfo(final GDItem.LabelInfo info) {
        this.setInfo(info, this.txtFullName);
    }

    public void setSetInfo(final GDItem.LabelInfo info) {
        this.setInfo(info, this.txtSet);
    }

    public void setSeedInfo(final GDItem.LabelInfo info) {
        this.setInfo(info, this.txtSeed);
    }

    public void setDescription(final String text) {
        this.txtDescription.setText(text);
    }

    public void setImage(final Icon icon) {
        this.lblImage.setIcon(icon);
    }

    public void setItemInfo(final GDItem item, final boolean isHardcore, final String charName) {
        if (item == null) {
            final GDItem.LabelInfo li = new GDItem.LabelInfo();
            li.text = "";
            this.setImage(null);
            this.setFullNameInfo(li);
            this.setSetInfo(li);
            this.setSeedInfo(li);
            this.pnlSimilar.setInfo(null);
            this.setDescription("");
        } else {
            this.setImage(item.getImageIcon());
            this.setFullNameInfo(item.getCompleteNameInfo(false, true));
            this.setSetInfo(item.getItemSetNameInfo());
            this.setSeedInfo(item.getSeedHexInfo());
            final DBStashItem.DuplicateInfo info = DBStashItem.determineSimilarItems(item, isHardcore, charName);
            this.pnlSimilar.setInfo(info);
            final String s = item.getFullDescription();
            this.setDescription(s);
        }
    }
}
